﻿Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web

Namespace DotNetNuke.Modules.ActiveForums.Controls
    Public Class ControlsBase
        Inherits ForumBase
        Private _template As String
        Private _templateFile As String
        Private _currentView As String = "forumview"
        Private _parseTemplate As Boolean = False
        <Description("Template for display"), PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property DisplayTemplate() As String
            Get
                If String.IsNullOrEmpty(_template) And Not String.IsNullOrEmpty(TemplateFile) Then
                    If Not String.IsNullOrEmpty(ControlConfig.TemplatePath) Then
                        _template = ControlConfig.TemplatePath & TemplateFile
                    Else
                        _template = TemplateFile
                    End If
                    _template = Utilities.GetTemplate(Page.ResolveUrl(_template))
                    _template = Utilities.ParseTokenConfig(_template, "default", ControlConfig)
                End If
                Return _template
            End Get
            Set(ByVal Value As String)
                _template = Value
            End Set
        End Property
        Public Property CurrentView() As String
            Get
                Return _currentView
            End Get
            Set(ByVal value As String)
                _currentView = value
            End Set
        End Property
        Public Property ParseTemplateFile() As Boolean
            Get
                Return _parseTemplate
            End Get
            Set(ByVal value As Boolean)
                _parseTemplate = value
            End Set
        End Property
        Public ReadOnly Property DataPageId() As Integer
            Get
                If HttpContext.Current.Request.QueryString(ParamKeys.PageId) Is Nothing Then
                    Return 1
                Else
                    Return Integer.Parse(HttpContext.Current.Request.QueryString(ParamKeys.PageId).ToString)
                End If
            End Get
        End Property
        Private Sub ControlsBase_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            If ParseTemplateFile Then
                If Not String.IsNullOrEmpty(DisplayTemplate) Then
                    Dim ctl As Control = Page.ParseControl(DisplayTemplate)
                    LinkControls(ctl.Controls)
                    Me.Controls.Add(ctl)
                End If



            End If
        End Sub
        Private Sub LinkControls(ByVal ctrls As ControlCollection)
            For Each ctrl As Control In ctrls
                If TypeOf ctrl Is Controls.ForumRow Then
                    DirectCast(ctrl, Controls.ForumRow).UserRoles = ForumUser.UserRoles
                End If
                If TypeOf ctrl Is Controls.ControlsBase Then
                    DirectCast(ctrl, Controls.ControlsBase).ControlConfig = Me.ControlConfig
                    DirectCast(ctrl, Controls.ControlsBase).ForumData = ForumData
                    DirectCast(ctrl, Controls.ControlsBase).ForumInfo = ForumInfo
                End If
                If ctrl.Controls.Count > 0 Then
                    LinkControls(ctrl.Controls)
                End If
            Next
        End Sub
    End Class
End Namespace